package com.iflytek.universitymanagement.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.domain.StudyHabit;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StudyHabitMapper extends BaseMapper<StudyHabit> {

//    // 查询所有学习习惯及学生姓名
//    @Select("SELECT sh.habit_id, sh.student_id, sh.habit_type, sh.habit_description, s.name AS student_name " +
//            "FROM study_habit sh " +
//            "JOIN student s ON sh.student_id = s.student_id")
//    List<StudyHabit> getAllHabitsWithStudentName();

    // 分页查询所有学习习惯及学生姓名
    @Select("SELECT sh.habit_id, sh.student_id, sh.habit_type, sh.habit_description, s.name AS student_name " +
            "FROM study_habit sh " +
            "JOIN student s ON sh.student_id = s.student_id")
    Page<StudyHabit> getAllHabitsWithStudentName(Page<StudyHabit> page);

    // 根据学生 ID 查询学习习惯及学生姓名
    @Select("SELECT sh.habit_id, sh.student_id, sh.habit_type, sh.habit_description, s.name AS student_name " +
            "FROM study_habit sh " +
            "LEFT JOIN student s ON sh.student_id = s.student_id " +
            "WHERE sh.student_id = #{studentId}")
    List<StudyHabit> getHabitsWithStudentName(Integer studentId);

    @Select("SELECT stu.*, s.name as studentName " +
            "FROM study_habit stu " +
            "LEFT JOIN student s ON stu.student_id = s.student_id ${ew.customSqlSegment}" )
    <T> IPage<StudyHabit> selectByPage(Page<T> tPage, @Param("ew") QueryWrapper<StudyHabit> queryWrapper);
}
